<template>
  <div class="article-create">
    <el-card>
      <el-input
        class="title-input"
        :placeholder="$t('msg.article.titlePlaceholder')"
        v-model="title"
        clearable
        maxlength="20"
      ></el-input>
      <el-tabs v-model="activeName">
        <el-tab-pane :label="$t('msg.article.markdown')" name="markdown">
          <Markdown
            :title="title"
            :detail="detailData"
            @onSuccess="handleSuccess"
          ></Markdown>
        </el-tab-pane>
        <el-tab-pane :label="$t('msg.article.richText')" name="editor">
          <Editor
            :title="title"
            :detail="detailData"
            @onSuccess="handleSuccess"
          ></Editor>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import Editor from './components/Editor'
import Markdown from './components/Markdown'
import { useRoute } from 'vue-router'
import { articleDetail } from '@/api/article'

const title = ref('')
const activeName = ref('markdown')

const handleSuccess = () => {
  title.value = ''
}

// 处理编辑相关
const route = useRoute()
const articleId = route.params.id
const detailData = ref({})
const getDetailData = async () => {
  detailData.value = await articleDetail(articleId)
  title.value = detailData.value.title
}

if (articleId) {
  getDetailData()
}
</script>

<style lang="scss" scoped>
.title-input {
  margin-bottom: 20px;
}
.el-input {
  border: 1px solid #ccc;
  border-radius: 6px;
}
</style>
